<?php 
namespace App\Services;
use App\Models\CollectionModel;

class CollectionService{
	/**
	 * 获取收藏详细信息
	 * @param int $member_id
	 * @param int $productId
     * @param string $type
	 * @return object $info
	 */
	public function getCollectionInfo($member_id,$productId,$type){
		$info = CollectionModel::where('member_id',$member_id)->where('product_id',$productId)->where('type',$type)
            ->first();
		return $info;
	}

	/**
	 * 添加收藏
	 * @param array $data
	 * @return bool $res
	 */
	public function insertCollection($data){
		$res = CollectionModel::insert($data);
		return $res;
	}

	/**
	 * 获取收藏列表
	 * @param int $member_id
	 * @return object $Collection_list
	 */
	public function getCollectionList($member_id){
		$collectionlist = CollectionModel::where('member_id',$member_id)->get();

		$courselist = array();
		$teaching_list = array();
		if(!empty($collectionlist)){
            foreach($collectionlist as $itm=>$lst){
                if($lst->type == 'course'){
                    $courselist[] = $lst;
                }

                if($lst->type == 'realia' || $lst->type == 'realias'){
                    $teaching_list[] = $lst;
                }
            }

            $Collection_list['course_list'] = $courselist;
            $Collection_list['teaching_list'] = $teaching_list;
        }else{
            $Collection_list['course_list'] = array();
            $Collection_list['teaching_list'] = array();
        }


		return $Collection_list;
	}

	/**
	 * 取消收藏
	 * @param int $id
	 * @return bool $res
	 */
	public function delCollection($id){
		$res = CollectionModel::where('id',$id)->delete();
		return $res;
	}
}